﻿@{
    Layout = null;
}
@using PermissionManager.Model;
@{
    var modules = ViewBag.Modules as List<Module>;
    var RoleId = ViewBag.RoleId;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="renderer" content="webkit|ie-comp|ie-stand" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link href="~/assets/css/bootstrap.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="~/css/style.css" />
    <link href="~/assets/css/codemirror.css" rel="stylesheet" />
    <link rel="stylesheet" href="~/assets/css/ace.min.css" />
    <link rel="stylesheet" href="~/font/css/font-awesome.min.css" />
    <link href="~/layui/css/layui.css" rel="stylesheet" />

    <link rel="stylesheet" href="~/Widget/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css" />

    <!--[if lte IE 8]>
      <link rel="stylesheet" href="assets/css/ace-ie.min.css" />
    <![endif]-->
    <script src="~/js/jquery-1.9.1.min.js"></script>
    <script src="~/assets/js/bootstrap.min.js"></script>
    <script src="~/assets/js/typeahead-bs2.min.js"></script>
    <script src="~/assets/js/jquery.dataTables.min.js"></script>
    <script src="~/assets/js/jquery.dataTables.bootstrap.js"></script>
    <script src="~/assets/layer/layer.js" type="text/javascript"></script>
    <script src="~/assets/laydate/laydate.js" type="text/javascript"></script>
    <script src="~/js/dragDivResize.js" type="text/javascript"></script>
    <script src="~/layui/layui.js"></script>

    <script type="text/javascript" src="~/Widget/zTree/js/jquery.ztree.all-3.5.min.js"></script>
    <script src="~/js/lrtk.js" type="text/javascript"></script>
    <title>修改权限</title>
</head>

<body>
    <div class="Competence_add_style clearfix">
        <div class="left_Competence_add">
            <div class="title_name">修改权限</div>
            <div class="Competence_add">
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 权限名称 </label>
                    <div class="col-sm-9"><input type="text" id="RoleName" placeholder="" name="权限名称" class="col-xs-10 col-sm-5" /></div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="form-field-1">所属组</label>
                    <div class="col-sm-9" style="margin-left:10px">
                        <select id="RoleModule">
                            @foreach (var item in modules)
                            {
                                <option value=@item.Id>@item.Name</option>
                            }
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 权限描述 </label>
                    <div class="col-sm-9"><textarea name="权限描述" class="form-control" id="RoleRe" placeholder="" onkeyup="checkLength(this);"></textarea><span class="wordage">剩余字数：<span id="sy" style="color:Red;">200</span>字</span></div>
                </div>
                <!--按钮操作-->
                <div class="Button_operation">
                    <button id="article_save_submit" class="btn btn-primary radius" type="submit"><i class="fa fa-save "></i> 保存并提交</button>
                    <button onclick="article_save();" class="btn btn-secondary  btn-warning" type="button"><i class="fa fa-reply"></i> 返回上一步</button>
                    <button onclick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
                </div>
            </div>
        </div>
        <!--权限分配-->
        <div class=" clearfix">
            <div id="category">
                <div id="scrollsidebar" class="left_Treeview">
                    <div class="show_btn" id="rightArrow"><span></span></div>
                    <div class="widget-box side_content">
                        <div class="side_title"><a title="隐藏" class="close_btn"><span></span></a></div>
                        <div class="side_list">
                            <div class="widget-header header-color-green2">
                                <h4 class="lighter smaller">权限分配</h4>
                            </div>
                            <div class="widget-body">
                                <div class="widget-main padding-8">
                                    <div id="treeDemo" class="ztree"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    //初始化宽度、高度
    $(".widget-box").height($(window).height());
    //当文档窗口发生改变时 触发
    $(window).resize(function () {
        $(".widget-box").height($(window).height());
    })
    /*字数限制*/
    function checkLength(which) {
        var maxChars = 200; //
        if (which.value.length > maxChars) {
            layer.open({
                icon: 2,
                title: '提示框',
                content: '您出入的字数超多限制!',
            });
            // 超过限制的字数了就将 文本框中的内容按规定的字数 截取
            which.value = which.value.substring(0, maxChars);
            return false;
        } else {
            var curr = maxChars - which.value.length; //250 减去 当前输入的
            document.getElementById("sy").innerHTML = curr.toString();
            return true;
        }
    };

    $(function () {
        /*得到角色信息*/
        $.ajax({
            type: "post",
            url: "/Role/GetRole?Id=" +@RoleId,
            dataType: 'json',
            success: function (d) {
                $("#RoleName").val(d.Name);
                $("#RoleRe").val(d.Remark);
                GetModule(@RoleId);
            }
        });
        /*查询角色对应的组*/
        function GetModule(RoleId) {
            if (RoleId == undefined) {
                RoleId = 1;
            }
            $.ajax({
                async: false,
                url: "/Role/GetModule",
                data: { RoleId: RoleId },
                type: "post",
                success: function (data) {
                    if (data.Success) {
                        $("#RoleModule").val(data.Message);
                    }
                }
            });
        }
        $("#category").fix({
            float: 'right',
            //minStatue : true,
            skin: 'green',
            durationTime: false
        });
    });

    var setting = {
        view: {
            dblClickExpand: false,
            showLine: false,
            selectedMulti: false
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootParentId: ""
            }
        },
        check: {
            chkStyle: "checkbox",
            chkboxType: { "Y": "ps"},
            enable: true
        },
        callback: {
            beforeClick: function (treeId, treeNode) {
            }
        }
    };
    /*得到对应角色功能数据*/
    var nodes = [];
    $(function () {
        $.ajax({
            type: "post",
            url: "/Role/GetFunctionsByUpdate?Id="+@RoleId,
            dataType: 'json',
            success: function (d) {
                // get the top level nodes
                for (var i = 0; i < d.data.length; i++) {
                    var row = d.data[i];
                    nodes.push({
                        id: row.Id,
                        name: row.Name,
                        pId: row.ParentId,
                        remark: row.Remark,
                        sortId: row.SortId,
                        checked:row.IsChecked
                    });
                }

                var t = $("#treeDemo");
                t = $.fn.zTree.init(t, setting, nodes);
                //$("#testIframe").bind("load", loadReady);
                //var zTree = $.fn.zTree.getZTreeObj("tree");
                //zTree.selectNode(zTree.getNodeByParam("id",'11'));
            }
        })

    });

    var ChangeNode = [];//存储改变的节点
    //得到修改的节点
    function GetChangeNode() {
         var zTree = $.fn.zTree.getZTreeObj("treeDemo");
         var changedNodes = zTree.getChangeCheckedNodes(); //获取改变的全部结点
        for ( var i=0 ; i < changedNodes.length ; i++ ){
            var treeNode = changedNodes[i];
            var fun = {};
            //判断是否为功能
            if (treeNode.id<0) {
                fun = {
                    FunId: -treeNode.id,
                    Id: treeNode.pId,
                    IsChecked:treeNode.checked
                }
                ChangeNode.push(
                    fun
                );
            }
	        }
    }

    layui.use(['table', 'form'], function () {
        var table = layui.table,
            form = layui.form;
        /*保存提交*/
        $("#article_save_submit").click(function () {

            GetChangeNode();
            var RoleName = $("#RoleName").val();
            var RoleModule = $("#RoleModule").val();
            var RoleRe = $("#RoleRe").val();

            if (RoleName != "") {

                $.ajax({
                    url: "/Role/UpdateRole",
                    data: {
                        Id:@RoleId,
                        Name: RoleName,
                        Remark: RoleRe, ModuleId: RoleModule,
                        MenuFun: JSON.stringify(ChangeNode)
                    },
                    type: "post",
                    success: function (data) {
                        if (data.Success) {
                            layer.alert('修改成功！', {
                                title: '提示框',
                                icon: 6,
                            });
                        } else {
                            layer.alert('修改失败！', {
                                title: '提示框',
                                icon: 5,
                            });
                        }
                    }
                });
            } else {
                layer.alert("权限名称不能为空");
            }
        })


    })

</script>